#!/bin/sh

## chang here
SERVICE_DIR=/opt/dmscloud
EUREKA_URL=
CONFIG_PROFILE=prod
JOB_CONFIG_PROFILE=db,mq,jobcloud,redis,job
JAVA_OPTIONS="-Xms4g -Xmx4g -Dactivejdbc.log"
JAVA_OPTS="-Dskywalking.plugin.jdbc.sql_body_max_length=3072 -Dskywalking.plugin.kafka.bootstrap_servers=kafka_ip:9092 -Dskywalking.agent.span_limit_per_segment=2000"
export JAVA_HOME=/opt/jdk1.8.0_192
export JRE_HOME=${JAVA_HOME}/jre

apps[0]=dmscloud-basedata-0.0.2-SNAPSHOT.jar
apps[1]=dmscloud-service-0.0.2-SNAPSHOT.jar


## 启动函数
startFun(){
    local SERVICE_NAME=$1

    if echo "${apps[@]}" | grep -w "${SERVICE_NAME}" &>/dev/null; then
        echo ">>>>"
    else
        echo ">>>>${SERVICE_NAME}不存在"
        exit 1
    fi

    local procedure=`ps -ef | grep -w "${SERVICE_DIR}/${SERVICE_NAME}" |grep -w "java"| grep -v "grep" | awk '{print $2}'`

    SKY_AGENT="-javaagent:/opt/dmscloud/skywalking-agent/skywalking-agent.jar"
    SKY_SVC="skywalking.collector.backend_service=proxy:11800"
    SKY_NAME="skywalking.agent.service_name=dmscloud@${SERVICE_NAME%-0.0.2*}"
    SKY_INSTANCE="skywalking.agent.instance_name=dmscloud@${SERVICE_NAME%-0.0.2*}@host_ip"
    if [ "${procedure}" = "" ];
    then
            echo ">>>>"
            echo ">>>> start ${SERVICE_NAME}..."
            echo ">>>> ${EUREKA_URL}  ${CONFIG_PROFILE}"
            cd ${SERVICE_DIR}
            if [ "${SERVICE_NAME}" = "dmscloud-job-0.0.2-SNAPSHOT.jar" ];
            then
                exec nohup ${JRE_HOME}/bin/java ${SKY_AGENT} -D${SKY_NAME} -D${SKY_INSTANCE} -D${SKY_SVC} ${JAVA_OPTS} ${JAVA_OPTIONS} -jar -Deureka.client.serviceUrl.defaultZone=${EUREKA_URL} -Dspring.cloud.config.profile=${CONFIG_PROFILE} -Dspring.cloud.config.name=${JOB_CONFIG_PROFILE} ${SERVICE_DIR}/${SERVICE_NAME}  >/dev/null 2>&1 &
		echo ">>>start job"
	    else
                exec nohup ${JRE_HOME}/bin/java ${SKY_AGENT} -D${SKY_NAME} -D${SKY_INSTANCE} -D${SKY_SVC} ${JAVA_OPTS} ${JAVA_OPTIONS} -jar -Deureka.client.serviceUrl.defaultZone=${EUREKA_URL} -Dspring.cloud.config.profile=${CONFIG_PROFILE} ${SERVICE_DIR}/${SERVICE_NAME}  >/dev/null 2>&1 &
            fi
            echo ">>>> start ${SERVICE_NAME} success"
            echo ">>>>"
    else
            echo ">>>> ${SERVICE_NAME} is start"
    fi
}

##停止函数
stopFun(){
    local SERVICE_NAME=$1
    
    if echo "${apps[@]}" | grep -w "${SERVICE_NAME}" &>/dev/null; then
        echo ">>>>"
    else
        echo ">>>>${SERVICE_NAME}不存在"
        exit 1
    fi
    local procedure=`ps -ef | grep -w "${SERVICE_DIR}/${SERVICE_NAME}" |grep -w "java"| grep -v "grep" | awk '{print $2}'`
    if [ "${procedure}" = "" ];
    then
            echo ">>>> ${SERVICE_NAME} is stop"
    else
            kill -9 ${procedure}
            sleep 1
            local argprocedure=`ps -ef | grep -w "${SERVICE_DIR}/${SERVICE_NAME}" |grep -w "java"| grep -v "grep" | awk '{print $2}'`
            if [ "${argprocedure}" = "" ];
            then
                    echo ">>>> ${SERVICE_NAME} stop success"
            else
                    kill -9 ${argprocedure}
                    echo ">>>> ${SERVICE_NAME} stop error"
            fi
    fi
}


case "$1" in 
        start)
                if [ $2 = "all" ];
                then
                        for app in ${apps[@]}
                        do
                          startFun $app
                        done
                else
                        startFun $2
                fi
                ;;
        stop)
                if [ $2 = "all" ];
                then
                        for app in ${apps[@]}
                        do
                          stopFun $app
                        done
                else
                        stopFun $2
                fi
                ;;
        restart)
                if [ $2 = "all" ];
                then
                        for app in ${apps[@]}
                        do
                          echo ">>>>>>>>>>>>>>>>>>>>>>>>"
                          stopFun $app
                          sleep 1
                          startFun $app
                          echo ">>>>>>>>>>>>>>>>>>>>>>>>"
                        done
                else
                        echo ">>>>>>>>>>>>>>>>>>>>>>>>"
                        stopFun $2
                        sleep 1
                        startFun $2
                        echo ">>>>>>>>>>>>>>>>>>>>>>>>"
                fi
                ;;  
        *)
                echo "usage: $1 [start|stop|restart]"
                ;;  
esac
